package com.yongcheon.sms_manager.data.sms_spam;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.CursorAdapter;

import com.yongcheon.sms_manager.ui.sms_spam.list.SPAMCurosrAdapter;
import com.yongcheon.sms_manager.utils.StringUtil;
import com.yongcheon.sms_manager.vo.SMSVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-26.
 */
public class SpamData extends AbstractSMSData {
    Context context;

    public SpamData(Context context){
        this.context = context;
    }

    public CursorAdapter getCurosrAdapter(Context context, SQLiteDatabase database){
        String sql = "Select * from spam_sms order by date desc";
        Cursor cursor = database.rawQuery(sql, null);
        CursorAdapter adapter = new SPAMCurosrAdapter(context, cursor);
        return adapter;
    }

    public void insert(SMSVO vo, Context context, SQLiteDatabase database){
        String sql = "Insert into spam_sms (thread_id, address, date, protocol, read, status, type, reply_path_present,subject, body, service_center, locked, error_code, seen, spam_desc, spam_category) values "+
                     "("+vo.getThread_id()+",'"+vo.getAddress()+"',"+vo.getDate()+","+vo.getProtocol()+","+vo.getRead()+","+vo.getStatus()+","+vo.getType()+","+vo.getReply_path_present()+
                     ",'"+ StringUtil.changeSpecialLettersForSQL(vo.getSubject())+"','"+StringUtil.changeSpecialLettersForSQL(vo.getBody())+"','"+vo.getService_center()+"',"+vo.getLocked()+","+
                      vo.getError_code()+","+vo.getSeen()+",'"+vo.getSpam_desc()+"','"+vo.getSpam_category()+"')";
        database.execSQL(sql);
    }

    public void delete(long id, Context context, SQLiteDatabase database){
        String sql = "Delete From spam_sms Where _id = "+id;
        database.execSQL(sql);
    }

    public ArrayList<SMSVO> select(String poneNum, Context context, SQLiteDatabase database){
        String sql = null;
        if(poneNum != null){
            sql = "Select * from spam_sms Where address = '" + poneNum+"' order by date desc";
        }else{
            sql = "Select * from spam_sms";
        }
        Cursor cursor = database.rawQuery(sql, null);
        return super.getSMSListByNum(cursor);
    }

    public void deleteByPhoneNum(String poneNum, Context context, SQLiteDatabase database){
        String sql =  "Delete From spam_sms Where address = '"+poneNum+"'";
        database.execSQL(sql);
    }
}
